package com.pactera.integral.controller;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pactera.integral.common.web.GenericController;
import com.pactera.integral.common.web.Response;
import com.pactera.integral.entity.Department;
import com.pactera.integral.service.DepartmentService;

@CrossOrigin
@Controller
@RequestMapping("adm/department")
public class DepartmentController extends GenericController{
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 部门列表
	 * @param param
	 * @param pageable
	 * @return Response
	 */
	@PostMapping("/page")
	@ResponseBody  
    public Response getDepartmentPage(@RequestBody HashMap<String,Object> param, Pageable pageable) {
    	Page<Department> page = departmentService.pageDepartment(param, pageable);
		
    	return this.normal(page); 
	}

	/**
	 * 添加或修改部门
	 * @param department
	 * @return Response
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	@ResponseBody
    public Response save(@RequestBody Department department) {
		try {
			departmentService.save(department);
			return this.normal(); 
		} catch (Exception ex) {
			return this.error(ex.getMessage());
		} 
    }
	
	/**
	 * 部门明细
	 * @param departmentId
	 * @return Response
	 */
	 @RequestMapping(value = "/departmentDetails",method = RequestMethod.GET)
	 @ResponseBody
	 public Response getDepartment(@RequestParam(value = "departmentId", required = false) String departmentId) {
		 
		 return this.normal(departmentService.getDepartment(departmentId)); 
	    } 
	
	 /**
	  * 部门下拉列表
	  * @return
	  */
	 @RequestMapping(value = "/list",method = RequestMethod.GET)
	 @ResponseBody
	 public Response getList() {
		
		 return this.normal(departmentService.departmentList()); 
	    }

	 /**
		 * 删除用户
		 * 
		 * @param userId
		 * @return Response
		 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public Response delete(@RequestParam(value = "departmentId", required = false) String departmentId) {

		return this.normal(departmentService.deleteDepartment(departmentId));
	}

}
